﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Threading.Tasks;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsfoodbarcodesServices : Repository<corecmsfoodbarcodes>, IcorecmsfoodbarcodesServices
    {
        public bool InsertAsync(corecmsfoodbarcodes entity)
        {
            return base.Change<corecmsfoodbarcodes>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsfoodbarcodes> entity)
        {
            return base.InsertOrUpdate(entity);
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfoodbarcodes>().Update(a => new corecmsfoodbarcodes()
            {
                deleteFlag = true
            }, a => a.barcodeId == id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsfoodbarcodes>().Update(a => new corecmsfoodbarcodes()
                {
                    deleteFlag = true
                }, a => a.barcodeId == id);
            }
            return returnKey;
        }


        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodbarcodes> GetCaChe()
        {
            return base.GetList();
        }

        #endregion


        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodbarcodes> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsfoodbarcodes> custos = new List<corecmsfoodbarcodes>();
            var where = Expressionable.Create<corecmsfoodbarcodes>();
            where = where.And(a => a.deleteFlag == false);
            if (pageIndex >= 1 && pageSize > 0)
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .OrderBy(a => a.barcodeId).ToList();
            }
            else
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.barcodeId).ToList();
            }
            return custos;
        }
        #endregion

        #region 根据类型查询数据
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodbarcodes> QueryAsync(long typeId)
        {
            List<corecmsfoodbarcodes> list = new List<corecmsfoodbarcodes>();
            var where = Expressionable.Create<corecmsfoodbarcodes>();
            where = where.And(a => a.deleteFlag == false && a.barcodeId > 0);
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.barcodeId).ToList();
            return list;
        }
        #endregion
    }
}